<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-book :to="{ path: '/home' }">首页</el-breadcrumb-book>
      <el-breadcrumb-book>{{"->"}}图书管理</el-breadcrumb-book>
      <el-breadcrumb-book>{{"->"}}图书列表</el-breadcrumb-book>
    </el-breadcrumb>

    <!-- 定义卡片 -->
    <el-card class="box-card">

      <!-- 定义第一行数据 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryBookInfo.query" clearable @clear="getBookList">
            <el-button slot="append" icon="el-icon-search" @click="getBookList"></el-button>
          </el-input>
        </el-col>
        <!-- <el-col :span="4">
          <el-button type="primary" @click="toAddBook">添加书籍</el-button>
        </el-col> -->
      </el-row>
      <el-table :data="bookList" style="width: 100%" stripe border>
        <!-- 获取商品列表数据 -->

        <el-table-column type="index" align="center" label="序号" width="80px">
        </el-table-column>

        <el-table-column prop="stage" align="center" label="图书所在类" width="80px">
        </el-table-column>

        <el-table-column prop="name" align="center" label="书名" width="100px">
        </el-table-column>

        <el-table-column prop="status" align="center" label="状态" width="80px">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"
              @change="updateStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>

        <el-table-column prop="author" align="center" label="作者名" width="100px">
        </el-table-column>

<!--        <el-table-column prop="authorAddr" align="center" label="作者简介地址" width="100px">
        </el-table-column> -->

        <el-table-column prop="price" align="center" label="价格(元)" width="80px">
          <template slot-scope="scope">
            {{scope.row.price | priceFormat}}
          </template>
        </el-table-column>

        <el-table-column prop="uploader" align="center" label="上传者" width="80px">
        </el-table-column>

<!--        <el-table-column prop="zan" align="center" label="点赞数" width="80px">
        </el-table-column> -->

        <el-table-column prop="rate" align="center" label="豆瓣评分" width="80px">
        </el-table-column>

<!--        <el-table-column prop="rateAddr" align="center" label="豆瓣评分地址" width="80px">
        </el-table-column> -->

        <el-table-column prop="downAddr" align="center" label="下载地址" width="80px">
        </el-table-column>

        <el-table-column prop="downNum" align="center" label="下载数量" width="80px">
        </el-table-column>

        <el-table-column prop="intro" align="center" label="简介" width="120px">
        </el-table-column>

<!--        <el-table-column prop="faceAddr" align="center" label="图书封面地址" width="200px">
        </el-table-column> -->
         <!-- 使用过滤器修改时间 在main.js中定义过滤器 -->
        <el-table-column prop="created" align="center" label="上传时间" width="200px">
          <template slot-scope="scope">
            <!-- {{scope.row.price | priceFormat}} -->
            {{scope.row.created | dateYMDHMSFormat}}
          </template>
        </el-table-column>

        <el-table-column prop="updated" align="center" label="修改时间" width="200px">
          <template slot-scope="scope">
            <!-- {{scope.row.price | priceFormat}} -->
            {{scope.row.updated | dateYMDHMSFormat}}
          </template>
        </el-table-column>

       <!-- <el-table-column prop="updated" label="更新时间" width="180px" :formatter="formatDate">
        </el-table-column> -->
        <el-table-column label="操作" align="center" width="200px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="updateBookBtn(scope.row)">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteBookBtn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <!-- 定义分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryBookInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryBookInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>


    <!-- 定义修改页面 当对话框关闭时,将表格数据重置-->
    <el-dialog title="修改图书信息" :visible.sync="updateDialogVisible" width="50%" @close="closeUpdateDialog">
      <el-form :model="updateBookModel" ref="updateBookRef" label-width="100px">
        <el-form-book label="图书所在级" prop="stage">
          图书所在级:
          <el-input v-model="updateBookModel.stage" ></el-input>
        </el-form-book>
        <el-form-book label="书名" prop="name">
          书名:
          <el-input v-model="updateBookModel.name" ></el-input>
        </el-form-book>

        <el-form-book label="作者" prop="author">
          作者:
          <el-input v-model="updateBookModel.author" ></el-input>
        </el-form-book>
        <el-form-book label="作者简介地址" prop="author_addr">
          作者简介地址:
          <el-input v-model="updateBookModel.authorAddr" ></el-input>
        </el-form-book>

        <el-form-book label="价格" prop="price">
          价格:
          <el-input v-model="updateBookModel.price"></el-input>
        </el-form-book>

        <el-form-book label="豆瓣评分" prop="rate">
          豆瓣评分:
          <el-input v-model="updateBookModel.rate" ></el-input>
        </el-form-book>
        <el-form-book label="豆瓣评分地址" prop="rate_addr">
          豆瓣评分地址:
          <el-input v-model="updateBookModel.rateAddr" ></el-input>
        </el-form-book>

        <el-form-book label="下载地址" prop="down_addr">
          下载地址:
          <el-input v-model="updateBookModel.downAddr"></el-input>
        </el-form-book>
        <el-form-book label="下载量" prop="down_num">
          下载量:
          <el-input v-model="updateBookModel.downNum" ></el-input>
        </el-form-book>

        <el-form-book label="简介" prop="intro">
          简介:
          <el-input v-model="updateBookModel.intro"></el-input>
        </el-form-book>
        <el-form-book label="图书封面地址" prop="face_addr">
          封面地址:
          <el-input v-model="updateBookModel.faceAddr"></el-input>
        </el-form-book>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false" >取 消</el-button>
        <el-button type="primary" @click="updateBook">确 定</el-button>
      </span>
    </el-dialog>





  </div>
</template>

<script>
  export default {
    data() {
      return {
        //1.定义商品列表信息
        bookList: [],
        //2.定义分页对象
        queryBookInfo: {
          query: '', //定义查询参数
          pageNum: 1,
          pageSize: 10
        },
        total: 0,

        updateDialogVisible: false,
        //定义修改用户数据封装
        updateBookModel: {}
      }
    },
    created() {
      //1.获取商品列表数据
      this.getBookList()
    },
    methods: {
      //实现商品信息分页查询
      async getBookList() {
        const {
          data: result
        } = await this.$http.get("/book/getBookList", {
          params: this.queryBookInfo
        })
        if (result.status !== 200) return this.$message.error("书籍列表查询失败")
        this.bookList = result.data.rows
        this.total = result.data.total
      },

      //条数变化时 调用
      handleSizeChange(size) {
        this.queryBookInfo.pageSize = size
        this.getBookList()
      },
      //页码值变化时 调用
      handleCurrentChange(current) {
        this.queryBookInfo.pageNum = current
        this.getBookList()
      },
      async updateStatus(book) {
        const {
          data: result
        } = await this.$http.put(`/book/updateBookStatus/${book.id}/${book.status}`)
        if (result.status !== 200) return this.$message.error("更新状态失败")
        this.$message.success("更新状态成功")
      },

      async deleteBookBtn(book) {
        //消息确认框
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //根据id删除数据
          const {
            data: result
          } = await this.$http.delete("/book/deleteBookById", {
            params: {
              id: book.id
            }
          })
          if (result.status !== 200) return this.$message.error("商品删除失败")
          this.$message.success("商品删除成功")
          //重新获取商品列表信息
          this.getBookList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },

      //修改信息
      async updateBookBtn(book){
        this.updateDialogVisible = true
        const {data: result} = await this.$http.get("/book/"+book.id)
        if(result.status !== 200) return this.$message.error("用户查询失败")
        this.updateBookModel = result.data
      },
      closeUpdateDialog(){
        //重置表格数据
        this.$refs.updateBookRef.resetFields()
      },
      updateBook(){
        //1.预校验数据
        this.$refs.updateBookRef.validate(async valid => {
           if(!valid)  return this.$message.error("表单验证没有通过")
           //根据接口文档要求封装数据
           let book = {}
           book.id = this.updateBookModel.id
           book.stage = this.updateBookModel.stage
           book.name = this.updateBookModel.name
           book.author = this.updateBookModel.author
           book.authorAddr = this.updateBookModel.authorAddr
           book.price = this.updateBookModel.price
           book.rate = this.updateBookModel.rate
           book.rateAddr = this.updateBookModel.rateAddr
           book.downAddr = this.updateBookModel.downAddr
           book.downNum = this.updateBookModel.downNum
           book.intro = this.updateBookModel.intro
           book.faceAddr = this.updateBookModel.faceAddr
           const {data: result} = await this.$http.put(`/book/updateBook`,book)
           if(result.status !== 200) return this.$message.error("用户修改失败")
           this.$message.success("用户更新成功")
           this.updateDialogVisible = false
           this.getBookList()
        })
      },

    },
    //利用钩子函数实现数据查询
    mounted(){
      this.getBookList()
    }
  }
</script>
<!-- 防止组件样式冲突 -->
<style lang="less" scoped>

</style>
